<!DOCTYPE HTML>
<html class="ui-page-login bg-fff">

	<head>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<title>LingPowerAI</title>
		<script type="text/javascript">
			document.write('<script src="../../js/fix.js?rd=?rd=' + Math.random() + '"><\/script>');
		</script>
		<script type="text/javascript">
			link(['../../css/reset.css']);
		</script>
	</head>

	<body class="bg-lan-default" style="display: none;">
		<header class="mui-bar mui-bar-nav">
			<h1 class="mui-title">登录</h1>
			<a class="mui-action-back mui-icon mui-icon mui-icon-closeempty mui-pull-right"></a>
		</header>
		<div class="mui-content mui-scroll-wrapper scroll">
			<div class="mui-scroll">
				<div class="loginformwarp">
					<div class="m1rem">
						<form class="mui-input-group">
							<div class="input-group">
								<div class="mui-input-row">
									<span class="mui-icon mui-icon-phone-filled"></span>
									<input value="" id='inpt_mobile' type="text" class="mui-input-clear mui-input" placeholder="请输入手机号">
								</div>
								<div class="mui-input-row">
									<span class="mui-icon mui-icon-locked"></span>
									<input value="" id='inpt_pwd' type="password" class="mui-input-clear mui-input" placeholder="请输入密码">
								</div>
							</div>
						</form>
						<div>
							<button id='btn_login' class="mui-btn mui-btn-block btn-ok">登录</button>
							<div class="linkArea">
								<a id='btn_forgetpwd' class="mui-pull-left">忘记密码?</a>
								<a id='btn_reg' class="mui-pull-right">新用户注册</a>
							</div>
						</div>
					</div>
					<div class="oauthwarp">
						<div class="oauth-title"><span>一键登录</span></div>
						<div id="oauth-area">
						</div>
				</div>
			</div>

		</div>
		<script type="text/javascript">
			//必须顺序加载
			script();
			// ['../../js/login/OAuth.js']
		</script>
		<script type="text/javascript">
			
			var backid = "index.html";
			var backurl = "../" + backid;

			mui.init({
				// beforeback: function() {
				// 	appPage.loginBack(backid,backurl)
				// }
			});
			mui.plusReady(function() {
        
				// initOauth();
				storage.init();
					if(localStorage.getItem("loginName")!=""){
					document.getElementById("inpt_mobile").value=localStorage.getItem("loginName");
					}
				// 一秒内连续点击2次返回键则退出程序
				var backButtonPress = 0;
				mui.back = function(event) {
					backButtonPress++;
					if (backButtonPress > 1) {
						plus.runtime.quit();
					} else {
						plus.nativeUI.toast('再按一次退出应用');
					}
					setTimeout(function() {
						backButtonPress = 0;
					}, 1000);
					return false;
				};

				backid = appPage.getParam("backid") || "index.html";
				backurl = "../" + backid;

				var btn_login = document.getElementById("btn_login");
				var inpt_mobile = document.getElementById("inpt_mobile");
				var inpt_pwd = document.getElementById("inpt_pwd");
				
				storageUser = kidstorageuser.getInstance();
				// alert(JSON.stringify(storageUser));
				//	inpt_mobile.value = storageUser.UserName;
				btn_login.addEventListener("tap", function() {
					inpt_mobile.blur();
					inpt_pwd.blur();
					if(document.getElementById("inpt_mobile").value!=""){
						localStorage.setItem("loginName",document.getElementById("inpt_mobile").value)
					}
					//模拟登陆
					// var data = {
					// 	PlayerId: 1,
					// 	Mobile: 18221786693,
					// 	NickName: '李小龙',
					// 	imgurl: "../../images/defuser.jpg",
					// 	SelfdomSign: '',
					// 	//cityid:data.CityId
					// }

					// storageUser.login(data);
					// storageUser.log();
					// appPage.loginBack(backid, backurl);
					// return;

					//实际登陆校验
					if (inpt_mobile.value.trim() == "") {
						appUI.showTopTip("请输入手机号");
						//mui.toast("请输入手机号");
						// inpt_mobile.focus();
					} else if (!ismobileno(inpt_mobile.value)) {
						appUI.showTopTip("手机号格式不正确");
						//mui.toast("手机号格式不正确");
					} else if (inpt_pwd.value.trim() == "") {
						appUI.showTopTip("请输入密码");
						//mui.toast("请输入密码");
						//inpt_pwd.focus();
					} else {

						var md5pwd = md5(inpt_pwd.value || "");
						appUI.setDisabled(btn_login);
						request("/cmdb/loginSales/", {
							// mobile: inpt_mobile.value,
							// pwd: md5pwd
 
							login_id: inpt_mobile.value,
							login_pwd: inpt_pwd.value
						}, function(json) {
							appUI.removeDisabled(btn_login);
							if (json[SysConstants.ERROR_CODE_KEY] == SysConstants.ERROR_CODE_OBJECT.ERROR_CODE_SUCCESS) {
								var data = json[SysConstants.DATASET_KEY];
								log(data);
								//封装用于存储到本地缓存里面的数据对象
								var storageData = {
									PlayerId: data.sales_id,
									Mobile: data.login_id,
									NickName: data.user_nm,
									imgurl: data.head_img,
									SelfdomSign: data.profile,
									tel_no:data.tel_no,
									wechat_no:data.wechat_no,
									wechat_img:data.wechat_img,
									sex:data.sex,
									duty:data.position_title,
									company:data.company,
									department:data.department,
									city:data.location_city,
									refer_code:data.refer_code
									//cityid:data.CityId
								}
								storageUser.login(storageData);
                               
								  plus.nativeUI.showWaiting("载入中...");
							mui.fire(plus.webview.getWebviewById("customer/customer.html"), 'refreshPage');
								  mui.fire(plus.webview.getWebviewById("home/home.html"), 'refreshPage');
								  mui.fire(plus.webview.getWebviewById("mine/mine.html"), 'refreshPage');
								  setTimeout(function(){
									  	plus.nativeUI.closeWaiting();
										openNew("../index.html")

								},1000);
								// setTimeout(function(){
								// 		appPage.loginBack(backid, backurl);
								// 	plus.nativeUI.closeWaiting();
								// },500);
							inpt_pwd.value="";

							} else {
								appUI.showTopTip(json[SysConstants.ERROR_MESSAGE_KEY]);
								//mui.toast(json[SysConstants.ERROR_MESSAGE_KEY]);
							}
						}, true, function() {
							appUI.removeDisabled(btn_login);
						});
					}
				});

				//注册
				document.getElementById("btn_reg").addEventListener("tap", function() {
					// mui.alert("当前系统尚未开放自主注册,请使用贵公司分配的账户信息登陆!");
					// return;
						let reg=plus.webview.create("reg.html","reg.html")
					setTimeout(function(){
						plus.webview.show(reg,"fade-in",100)
					},200);
					// openNew("reg.html");
					// mui.ope
				});

				//忘记密码
				document.getElementById("btn_forgetpwd").addEventListener("tap", function() {
					let forgepwd=plus.webview.create("forgetPwd.html","forgetPwd.html")
					setTimeout(function(){
						plus.webview.show(forgepwd,"fade-in",100)
					},200);
				});
				//手机登录
				// document.getElementById("btn_mobilelogin").addEventListener("tap", function() {
				// 	openNew("mobileLogin.html", {
				// 		backid: backid
				// 	});
				// });

			})
		</script>
	</body>

</html>
